﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVCClubSite.IDAL;
using MVCClubSite.Model;

namespace MVCClubSite.BLL
{
    /// <summary>
    /// This is the business component to manage user profiles
    /// </summary>
    public class UserProfilesService
    {
        IUserProfiles _userProfiles;

        /// <summary>
        /// Default constructor that will use the DAL set in the web config
        /// </summary>
        public UserProfilesService()
        {
            _userProfiles = MVCClubSite.DALFactory.DataAccess.CreateUserProfiles();
        }

        /// <summary>
        /// Constructor for testing with a different DAL
        /// </summary>
        /// <param name="announcements">DAL for the announcement service to use</param>
        public UserProfilesService(IUserProfiles userProfiles)
        {
            _userProfiles = userProfiles;
            if (_userProfiles == null)
                throw new InvalidOperationException("UserProfiles DAL cannot be null.");
        }

        /// <summary>
        /// Method to get user profiles
        /// </summary>
        /// <returns></returns>
        public List<UserProfile> GetUserProfiles()
        {
            return _userProfiles.GetUserProfiles().ToList();
        }

        /// <summary>
        /// Method to get a user's profile
        /// </summary>
        /// <param name="userID">Guid of the user</param>
        /// <returns>User profile business entity</returns>
        public UserProfile GetUserProfile(Guid userID)
        {
            UserProfile up = _userProfiles.GetUserProfile(userID);
            if (up == null)
            {
                UserProfile newUserProfile = new UserProfile();
                newUserProfile.UserID = userID;
                newUserProfile.LastUpdate = DateTime.Now;
                newUserProfile.LastActivity = DateTime.Now;
                _userProfiles.Save(newUserProfile);
                return newUserProfile;
            }
            return up;
        }

        /// <summary>
        /// Method to insert or update a user profile
        /// </summary>
        /// <param name="userProfile">Profile to insert or update</param>
        public void Save(UserProfile userProfile)
        {
            if (userProfile.Avatar != null)
                userProfile.Avatar = ImageHelper.MakeThumbnail(userProfile.Avatar, 70, 70);

            userProfile.LastUpdate = DateTime.Now;
            userProfile.LastActivity = DateTime.Now;
            _userProfiles.Save(userProfile);
        }

        /// <summary>
        /// Method to delete a user profile
        /// </summary>
        /// <param name="userID">Guid of the user to delete</param>
        public void Delete(Guid userID)
        {
            _userProfiles.Delete(userID);
        }
    }
}
